package com.wangzhen.spring.inject;

import com.wangzhen.spring.inject.holder.UserHolder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 演示Value注解
 * 1、在ValueAnnotationInjectDemo  这个类上只加入一个@PropertySource注解，貌似不起作用，必须加上@Configuration注解才行
 */
@Configuration
@PropertySource(value = "classpath:/META-INF/default.properties")
public class ValueAnnotationInjectDemo {

    @Value("${user.id}")
    private String userName;

    @Value("${user.age}")
    private Integer userAge;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(ValueAnnotationInjectDemo.class);
        // 启动应用上下文
        applicationContext.refresh();
        // 获取UserHolder
        ValueAnnotationInjectDemo demo = applicationContext.getBean(ValueAnnotationInjectDemo.class);
        System.out.println("demo.userAge: " +demo.userAge);
        System.out.println("demo.userAge: "+demo.userName);
        // 关闭应用上下文
        applicationContext.close();
    }
}
